# 设置正确的Java路径
$env:JAVA_HOME = "C:\Program Files\Java\jdk-21"

# 创建target目录
if(!(Test-Path "target\classes")){
    New-Item -ItemType Directory -Force -Path "target\classes"
}

# 查找所有Spring Boot和相关依赖的jar文件
$m2Repo = "$HOME\.m2\repository"
$dependencies = Get-ChildItem -Path $m2Repo -Recurse -Filter "*.jar" | Where-Object { 
    $_.FullName -like "*springframework*" -or 
    $_.FullName -like "*spring-boot*" -or 
    $_.FullName -like "*jackson*" -or 
    $_.FullName -like "*log4j*" -or 
    $_.FullName -like "*commons-io*" -or 
    $_.FullName -like "*pdfbox*" -or 
    $_.FullName -like "*tomcat*"
} | ForEach-Object { $_.FullName }

# 构建类路径
$classpath = ($dependencies -join ";") + ";src\main\java;src\main\resources"

# 编译Java源代码
Write-Host "正在编译Java源代码..."
& "$env:JAVA_HOME\bin\javac.exe" -encoding UTF-8 -cp "$classpath" -d "target\classes" "src\main\java\com\example\deepseek\backend\DeepSeekBackendApplication.java"

# 检查编译是否成功
if ($LASTEXITCODE -ne 0) {
    Write-Host "编译失败，请检查错误信息。" -ForegroundColor Red
    pause
    exit 1
}

# 如果编译成功，运行应用程序
Write-Host "编译成功，正在启动应用程序..." -ForegroundColor Green
& "$env:JAVA_HOME\bin\java.exe" -cp "$classpath;target\classes" com.example.deepseek.backend.DeepSeekBackendApplication

pause